package com.ruoyi.project.modules.pageImageElementCondition.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.pageImageElementDetail.domain.PageImageElementConditionDetail;
import com.ruoyi.project.modules.pageImageElementDetail.mapper.PageImageElementConditionDetailMapper;
import com.ruoyi.project.modules.pageImageElementRef.domain.PageImageElementRef;
import com.ruoyi.project.modules.pageImageElementRef.mapper.PageImageElementRefMapper;
import com.ruoyi.project.modules.processBranch.service.impl.ProcessBranchServiceImpl;
import com.ruoyi.project.modules.processBranchConditionDetail.domain.ProcessBranchConditionDetail;
import com.ruoyi.system.service.ISysDictTypeService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.pageImageElementCondition.mapper.PageImageElementConditionMapper;
import com.ruoyi.project.modules.pageImageElementCondition.domain.PageImageElementCondition;
import com.ruoyi.project.modules.pageImageElementCondition.service.IPageImageElementConditionService;
import com.ruoyi.common.core.text.Convert;

/**
 * 页面图片与元素条件Service业务层处理
 *
 * @author smallrain
 * @date 2022-05-06
 */
@Service
@AllArgsConstructor
public class PageImageElementConditionServiceImpl implements IPageImageElementConditionService {

    private PageImageElementConditionMapper pageImageElementConditionMapper;

    private PageImageElementConditionDetailMapper pageImageElementConditionDetailMapper;

    private ISysDictTypeService sysDictTypeService;

    private PageImageElementRefMapper pageImageElementRefMapper;
    /**
     * 查询页面图片与元素条件
     *
     * @param id 页面图片与元素条件主键
     * @return 页面图片与元素条件
     */
    @Override
    public PageImageElementCondition selectPageImageElementConditionById(Long id) {
        return pageImageElementConditionMapper.selectPageImageElementConditionById(id);
    }

    /**
     * 查询页面图片与元素条件列表
     *
     * @param pageImageElementCondition 页面图片与元素条件
     * @return 页面图片与元素条件
     */
    @Override
    public List<PageImageElementCondition> selectPageImageElementConditionList(PageImageElementCondition pageImageElementCondition) {
        return pageImageElementConditionMapper.selectPageImageElementConditionList(pageImageElementCondition);
    }

    /**
     * 新增页面图片与元素条件
     *
     * @param pageImageElementCondition 页面图片与元素条件
     * @return 结果
     */
    @Override
    public int insertPageImageElementCondition(PageImageElementCondition pageImageElementCondition) {
        return pageImageElementConditionMapper.insertPageImageElementCondition(pageImageElementCondition);
    }

    /**
     * 修改页面图片与元素条件
     *
     * @param pageImageElementCondition 页面图片与元素条件
     * @return 结果
     */
    @Override
    public int updatePageImageElementCondition(PageImageElementCondition pageImageElementCondition) {
        return pageImageElementConditionMapper.updatePageImageElementCondition(pageImageElementCondition);
    }

    /**
     * 批量删除页面图片与元素条件
     *
     * @param ids 需要删除的页面图片与元素条件主键
     * @return 结果
     */
    @Override
    public int deletePageImageElementConditionByIds(String ids) {
        return pageImageElementConditionMapper.deletePageImageElementConditionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除页面图片与元素条件信息
     *
     * @param id 页面图片与元素条件主键
     * @return 结果
     */
    @Override
    public int deletePageImageElementConditionById(Long id) {
        return pageImageElementConditionMapper.deletePageImageElementConditionById(id);
    }

    @Override
    public List<PageImageElementCondition> conditionList(Set<Long> ids) {
        return this.pageImageElementConditionMapper.conditionList(ids);
    }

    @Override
    public void saveCondition(PageImageElementCondition pageImageElementCondition) {
        List<SysDictData> sysDictDataList = sysDictTypeService.selectDictDataByType("storageType");
        Map<String, List<SysDictData>> dictCollect = sysDictDataList.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));

        List<SysDictData> elementConditionType = sysDictTypeService.selectDictDataByType("elementConditionType");
        Map<String, List<SysDictData>> collect = elementConditionType.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
        pageImageElementCondition.setName(collect.get(pageImageElementCondition.getElementConditionType()).get(0).getDictLabel());
        if (pageImageElementCondition.getId() == null) {
            this.pageImageElementConditionMapper.insertPageImageElementCondition(pageImageElementCondition);
        } else {
            this.pageImageElementConditionDetailMapper.deletePageImageElementConditionDetailByConditionId(pageImageElementCondition.getId());
        }

        List<SysDictData> judge_condition = this.sysDictTypeService.selectDictDataByType("JUDGE_CONDITION");
        Map<String, List<SysDictData>> sysDictDataMap = judge_condition.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));

        String branchConditionName = "";
        String joinCondition = " " + pageImageElementCondition.getJoinCondition() + " ";
        List<PageImageElementConditionDetail> pageImageElementConditionDetailList = pageImageElementCondition.getPageImageElementConditionDetailList();
        if (CollectionUtil.isNotEmpty(pageImageElementConditionDetailList)) {
            Map<String, List<PageImageElementConditionDetail>> record = new TreeMap<>();
            for (PageImageElementConditionDetail detail : pageImageElementConditionDetailList) {
                detail.setConditionId(pageImageElementCondition.getId());
                List<PageImageElementConditionDetail> detailList = record.computeIfAbsent(detail.getBranchConditionGroupId(), key -> new ArrayList<>());
                detailList.add(detail);
            }
            for (Map.Entry<String, List<PageImageElementConditionDetail>> stringListEntry : record.entrySet()) {
                List<PageImageElementConditionDetail> details = stringListEntry.getValue();
                String conditionName = "";
                for (int i = 0; i < details.size(); i++) {
                    PageImageElementConditionDetail detail = details.get(i);
                    if ("3".equals(detail.getSourceType()) && detail.getStorageType() != null) {
                        List<SysDictData> sysDictData = dictCollect.get(detail.getStorageType());
                        if (CollectionUtil.isNotEmpty(sysDictData)) {
                            conditionName = sysDictData.get(0).getDictLabel() + " ";
                        }
                    }
                    if (detail.getBranchConditionDetailDictCode().equals("3")) {
                        conditionName = conditionName + detail.getInterfaceName() + "(" + detail.getInterfaceUrl() +")" + ".出参";
                    } else {
                        conditionName = conditionName + detail.getInterfaceName() + "(" + detail.getInterfaceUrl() +")" + "." + detail.getObjectName() + "(" + detail.getObjectEnName() +")";
                        if (detail.getBranchConditionDetailDictCode().equals("1")) {
                            if (StringUtils.isNotEmpty(detail.getObjectFieldName())) {
                                conditionName = conditionName + "." + detail.getObjectFieldName() + "(" + detail.getObjectFieldEnName() +")";
                            }
                        } else {
                            conditionName = conditionName + "数据集条数";
                        }
                    }
                    if ("IS_NULL".equals(detail.getJudgeConditionDictCode())) {
                        conditionName = conditionName + ProcessBranchServiceImpl.convertDict("IS_NULL", sysDictDataMap);
                    } else if ("IS_NOT_NULL".equals(detail.getJudgeConditionDictCode())) {
                        conditionName = conditionName + ProcessBranchServiceImpl.convertDict("IS_NOT_NULL", sysDictDataMap);
                    } else if ("IS_TRUE".equals(detail.getJudgeConditionDictCode())) {
                        conditionName = conditionName + ProcessBranchServiceImpl.convertDict("IS_TRUE", sysDictDataMap);
                    } else if ("IS_FALSE".equals(detail.getJudgeConditionDictCode())) {
                        conditionName = conditionName + ProcessBranchServiceImpl.convertDict("IS_FALSE", sysDictDataMap);
                    } else {
                        conditionName = conditionName + ProcessBranchServiceImpl.convertDict(detail.getJudgeConditionDictCode(), sysDictDataMap) + (StringUtils.isNotEmpty(detail.getDictName()) ? detail.getDictName() : detail.getContractValue());
                    }
                    if (i == details.size() - 1) continue;
                    conditionName = conditionName + " " + detail.getJoinCondition() + " ";
                }

                if (details.size() > 1) {
                    conditionName = "(" + conditionName + ")";
                }
                branchConditionName = branchConditionName + conditionName + joinCondition;
            }
            this.pageImageElementConditionDetailMapper.batchSave(pageImageElementConditionDetailList);
        }

        PageImageElementRef pageImageElementRef = new PageImageElementRef();
        pageImageElementRef.setId(pageImageElementCondition.getPagImageElementRefId());
        List<PageImageElementRef> pageImageElementRefs = this.pageImageElementRefMapper.selectPageImageElementRefList(pageImageElementRef);
        branchConditionName = branchConditionName.substring(0, branchConditionName.length() - joinCondition.length());
        if (pageImageElementCondition.getDescription()== null) {
            pageImageElementCondition.setDescription(pageImageElementRefs.get(0).getPageElementName() + " " + pageImageElementCondition.getName() + ":" + branchConditionName);
        }
        pageImageElementCondition.setName(branchConditionName);
        this.pageImageElementConditionMapper.updatePageImageElementCondition(pageImageElementCondition);
    }
}
